﻿using System;
using System.Linq;
using STS.Common;
using STS.Interfaces;
using STS.Schema.Enums;
using STS.Schema.Factory.SqlServer;
using STS.Schema.Interfaces;
using STS.Schema.MSSql;

namespace STS.Schema.Factory
{
    public class SqlServer2008SchemaFactory : SchemaFactoryBase
    {
        public SqlServer2008SchemaFactory(DbServer server)
            : base(server)
        {

        }

       

        public override Type GetSqlObjectType(SqlObjectType sqlObjectType)
        {
            switch (sqlObjectType)
            {
                case SqlObjectType.UserTable: return typeof(MSSqlTable);
                case SqlObjectType.View: return typeof(MSSqlView);
                case SqlObjectType.Column: return typeof(MSSqlColumn);
                case SqlObjectType.Database: return typeof(MSSqlDatabase);
                case SqlObjectType.ForeignKey: return typeof(MSSqlForeignKey);
                case SqlObjectType.ForeignKeyColumn: return typeof(MSSqlForeignKeyColumn);
                case SqlObjectType.StoredProcedure: return typeof(MSSqlStoredProcedure);
                case SqlObjectType.TableFunction: return typeof(MSSqlTableValuedFunction);
                case SqlObjectType.ScalarFunction: return typeof(MSSqlScalarFunction);
                case SqlObjectType.Trigger: return typeof(MSSqlTrigger);
                default: return null;
            }
        }

        public override ISqlObjectCollection GetCollection<T>(ISqlObject parent = null)
        {
            Type[] interfaceTypes = typeof(T).GetInterfaces();
            if (interfaceTypes.Contains(typeof(IDatabase)))
            {
                return new SqlServer2008ObjectCollection<MSSqlDatabase>(parent);
            }
            if (interfaceTypes.Contains(typeof(ITable)))
            {
                return new SqlServer2008ObjectCollection<MSSqlTable>(parent);
            }
            if (interfaceTypes.Contains(typeof(IColumn)))
            {
                return new SqlServer2008ObjectCollection<MSSqlColumn>(parent);
            }
            if (interfaceTypes.Contains(typeof(IView)))
            {
                return new SqlServer2008ObjectCollection<MSSqlView>(parent);
            }
            if (interfaceTypes.Contains(typeof(IForeignKey)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlForeignKey>(parent);
            }
            if (interfaceTypes.Contains(typeof(IForeignKeyColumn)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlForeignKeyColumn>(parent);
            }
            if (interfaceTypes.Contains(typeof(IStoredProcedure)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlStoredProcedure>(parent);
            }
            if (interfaceTypes.Contains(typeof(IScalarFunction)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlScalarFunction>(parent);
            }
            if (interfaceTypes.Contains(typeof(ITableValuedFunction)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlTableValuedFunction>(parent);
            }
            if (interfaceTypes.Contains(typeof(ITrigger)))
            {
                return new SqlServer2008R2ObjectCollection<MSSqlTrigger>(parent);
            }

            throw new STSException(string.Format("Unknown server object {0}", typeof(T).Name));
        }
    }
}
